package edu.guigu.appquickstart.simpleview.subitem;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import edu.guigu.appquickstart.R;

public class SpinnerActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner);
        // Actionbar
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        spinnerDemo();
    }

    private void spinnerDemo() {
        // 下拉
        Spinner spinner = findViewById(R.id.spinner);
        // 准备好数据
        String[] datas = {"肖申克的救赎", "阿甘正传", "泰坦尼克号", "盗梦空间", "星际穿越", "楚门的世界", "无间道", "大话西游", "让子弹飞", "流浪地球"};
        // 准备好布局
        int layoutId = android.R.layout.simple_list_item_1;

        ArrayAdapter<String> adapter = new ArrayAdapter<>(
                this,
                layoutId,   // 布局
                datas       // 数据
        );

        // 设置适配器 (适配器内部工作核心：将数据适配到布局上)
        spinner.setAdapter(adapter);
        spinner.setPrompt("请选择您想看的电影"); // 此属性如果在xml中设置，只能访问string.xml中的值

        // 监听
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(SpinnerActivity.this, "选择了：" +position, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }


    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if(item.getItemId() == android.R.id.home){
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

}